<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>轮播图</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="addbgimg">添加轮播图</el-button>
      </div>
      <div>
        <el-table :data="rotationchartlist" style="width: 100%" stripe>
          <el-table-column fixed type="index" label="#" width="80">
          </el-table-column>
          <el-table-column label="轮播图">
            <template slot-scope="scope">
              <img :src="scope.row.image" alt="轮播图" class="rcimg_box">
            </template>
          </el-table-column>
          <el-table-column prop="title" label="标题" width="360">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="240">
            <template slot-scope="scope">
              <el-button @click="deleteRow(scope.row._id)" type="danger" size="small">
                移除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rotationchartlist: []
    }
  },
  created() {
    this.getrotationchart()
  },
  methods: {
    // 获取轮播图列表
    async getrotationchart() {
      const res = await this.$http.get('/slides')
      // console.log(res)
      if (res.status !== 200) {
        return this.$message({
          showClose: true,
          message: '轮播图列表获取失败!',
          type: 'error'
        })
      }
      this.rotationchartlist = res.data
    },
    addbgimg() {
      this.$router.push('/addbgimg')
    },
    async deleteRow(id) {
      const res = await this.$http({
        method: 'delete',
        url: '/slides/' + id
      })
      // console.log(res)
      if (res.status !== 200) {
        return this.$message({
          showClose: true,
          message: '删除轮播图失败!',
          type: 'error'
        })
      }
      this.$message({
        showClose: true,
        message: '删除轮播图成功!',
        type: 'success'
      })
      this.getrotationchart()
    }
  }
}
</script>

<style scoped>
.rcimg_box {
  width: 360px;
  height: 180px;
}
</style>
